package com.esun.rabbit2.viewer.mainact.lotteryhallact.sportteryfootballshengfu;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Hashtable;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.UserInfo;
import com.esun.model.sportteryfootball.SportteryFootballLotteryInfo;
import com.esun.model.sportteryfootballshengfu.SportteryFootballShengfuAgainstInfo;
import com.esun.model.sportteryfootballshengfu.SportteryFootballShengfuLotteryInfo;
import com.esun.network.communicater.UserCommunicater;
import com.esun.rabbit2.communicaters.sportteryfootballshengfu.SportteryFootballShengfuNormalBuyCommunicater;
import com.esun.rabbit2.viewer.LoginAct;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryBuyLottery;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseActivity;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryChooseList;
import com.esun.rabbit2.viewer.mainact.lotteryhallact.SportteryFootballShengfuAgp;
import com.esun.rabbit2.viewer.mainact.mylotteryact.recharg.RechargeMenuPage;
import com.esun.ui.R;
import com.esun.util.LoginTimeControl;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: SportteryFootballShengfuBuyLottery
 * @Description: 足球胜负投注页面
 * @author: Liujun liuj@500wan.com
 * @date: 2012-4-25 上午11:51:16
 * 
 */
public class SportteryFootballShengfuBuyLottery extends SportteryBuyLottery implements OnClickListener {

	private int zhushu;// 注数
	private int beishu;// 倍数
	private String codes; // 请求参数
	private Context context;
	private View view;
	private LayoutInflater inflater;
	private SportteryChooseList chooseListPage;

	// 界面控件 Button
	private Button btnBack; // 继续选队
	private Button btnClean; // 清除列表
	private Button btnHemai; // 发起合买
	private Button btnNormalBuy;// 立即购买

	// 界面控件 Textview
	private TextView tvBetCount; // 注数
	private TextView tvMoney; // 总金额
	private TextView tvBalance; // 投注后余额
	private EditText edtBeitou; // 倍数
	private TextView tvCountAndTime;// 已选场数以及投注截至时间
	private TextView tv_homeName; // 主队
	private TextView tv_guestteam; // 客队
	private TextView tv_qihaoName; // 序列号

	private LinearLayout linearList;
	private SportteryFootballShengfuNormalBuyCommunicater sportteryFootballShengfuNormalBuyCommunicater;// 胜负彩立即购买接口
	private UserCommunicater userCommunicater;// 获取个人信息刷新余额

	private ProgressDialog progressdialogNormalBuy;
	private Handler handlerBalance;
	private final static int GETBALANCE_SUCCEED = 1003; // 标识位,获取余额成功
	private final static int GETBALANCE_FAIL = 1004; // 标识位,获取余额失败
	private int allTeam = 14; // 选队的数目

	/** 构造函数 传入固定的参数 */
	public SportteryFootballShengfuBuyLottery(Context context, View view) {
		this.view = view;
		this.context = context;
		inflater = ((Activity) context).getLayoutInflater();

		init();
		handlerInit();
		initHandler();
		EDebug.o(this, "开始获取用户余额的线程1");
		threadGetUserBalance();
		EDebug.o(this, "开始获取用户余额的线程2");
		updateListView();
	}

	/** 数据初始化 */
	private void dataInit() {
		zhushu = 0;
		beishu = 1;
		codes = Config.NULLMARK;
		updateTextViewSet();
	}

	/** 更新主列表 */
	private void updateListView() {

		linearList.removeAllViews();
		LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		for (int i = 0; i < SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().size(); i++) {
			final int i_ = i;
			if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {

				SportteryFootballShengfuAgainstInfo sportteryFootballShengfuAgainstInfo = (SportteryFootballShengfuAgainstInfo) SportteryFootballShengfuLotteryInfo
						.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(INFO);
				View listItem = inflater.inflate(R.layout.sportteryfootballshengfu_buy_choose_act_list_item, null);
				listItem.setTag(i);
				tv_homeName = (TextView) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_tv_homeName);
				tv_homeName.setText(sportteryFootballShengfuAgainstInfo.getHometeam());
				tv_qihaoName = (TextView) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_tv_qihao);
				tv_qihaoName.setText((i_ + 1) + " " + " ");

				tv_guestteam = (TextView) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_tv_visiterName);
				tv_guestteam.setText(sportteryFootballShengfuAgainstInfo.getGuestteam());

				final CheckBox cbxW = (CheckBox) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_cbx_w);
				final CheckBox cbxD = (CheckBox) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_cbx_d);
				final CheckBox cbxL = (CheckBox) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_cbx_l);
				final TextView tvW = (TextView) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_tv_w);
				final TextView tvD = (TextView) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_tv_d);
				final TextView tvL = (TextView) listItem.findViewById(R.id.sportteryfootballshengfuchooseactlistitem_tv_l);

				cbxW.setChecked((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(W));
				if (cbxW.isChecked()) {
					tvW.setTextColor(context.getResources().getColor(R.color.shengfucai_check_text_color));

				} else {
					tvW.setTextColor(context.getResources().getColor(R.color.shengfucai_uncheck_text_color));

				}
				cbxD.setChecked((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(D));
				if (cbxD.isChecked()) {
					tvD.setTextColor(context.getResources().getColor(R.color.shengfucai_check_text_color));

				} else {
					tvD.setTextColor(context.getResources().getColor(R.color.shengfucai_uncheck_text_color));

				}
				cbxL.setChecked((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(L));
				if (cbxL.isChecked()) {
					tvL.setTextColor(context.getResources().getColor(R.color.shengfucai_check_text_color));

				} else {
					tvL.setTextColor(context.getResources().getColor(R.color.shengfucai_uncheck_text_color));

				}

				ArrayList<CheckBox> checkBoxTemp = new ArrayList<CheckBox>();
				checkBoxTemp.add(cbxW);
				checkBoxTemp.add(cbxD);
				checkBoxTemp.add(cbxL);

				OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						switch (buttonView.getId()) {
						case R.id.sportteryfootballshengfuchooseactlistitem_cbx_w:
							if (isChecked) {
								tvW.setTextColor(context.getResources().getColor(R.color.shengfucai_check_text_color));

							} else {
								tvW.setTextColor(context.getResources().getColor(R.color.shengfucai_uncheck_text_color));

							}
							updateLotteryListItem(i_, isChecked, cbxD.isChecked(), cbxL.isChecked());
							break;

						case R.id.sportteryfootballshengfuchooseactlistitem_cbx_d:
							if (isChecked) {
								tvD.setTextColor(context.getResources().getColor(R.color.shengfucai_check_text_color));

							} else {
								tvD.setTextColor(context.getResources().getColor(R.color.shengfucai_uncheck_text_color));

							}
							updateLotteryListItem(i_, cbxW.isChecked(), isChecked, cbxL.isChecked());
							break;

						case R.id.sportteryfootballshengfuchooseactlistitem_cbx_l:
							if (isChecked) {
								tvL.setTextColor(context.getResources().getColor(R.color.shengfucai_check_text_color));

							} else {
								tvL.setTextColor(context.getResources().getColor(R.color.shengfucai_uncheck_text_color));

							}
							updateLotteryListItem(i_, cbxW.isChecked(), cbxD.isChecked(), isChecked);
							break;

						default:
							break;
						}
					}
				};
				cbxW.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxD.setOnCheckedChangeListener(onCheckedChangeListener);
				cbxL.setOnCheckedChangeListener(onCheckedChangeListener);
				linearList.addView(listItem, param);
			}
		}
	}

	/** handler 处理 处理购买成功 失败 */
	private void handlerInit() {

		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				progressdialogNormalBuy.dismiss();
				switch (msg.what) {
				case BUY_SUCCEED:
					buySucceed();
					break;

				case BUY_FAIL:
					if (sportteryFootballShengfuNormalBuyCommunicater.getState() == 0) {
						// ViewerUtil.showTipDialog(context, sportteryFootballShengfuNormalBuyCommunicater.getErrorMSG() + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD,
						// false);
						Toast.makeText(context, sportteryFootballShengfuNormalBuyCommunicater.getServerMsg() + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, Toast.LENGTH_SHORT).show();
					} else if (sportteryFootballShengfuNormalBuyCommunicater.getState() == 105) {
						popBalanceNotEnoughAlert();
					} else {

						// ViewerUtil.showTipDialog(context, sportteryFootballShengfuNormalBuyCommunicater.getErrorMSG(), false);
						Toast.makeText(context, sportteryFootballShengfuNormalBuyCommunicater.getServerMsg(), Toast.LENGTH_SHORT).show();
					}

					break;

				case Config.CONNECTION_FAIL:
					// ViewerUtil.showTipDialog(context, Language.STR_CONNECTIONFAIL + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, false);
					Toast.makeText(context, Language.STR_CONNECTIONFAIL + Config.COMMA + Language.POPLOTTERYTIP_LOOKOVERRECORD, Toast.LENGTH_SHORT).show();
					break;

				default:
					break;
				}
			}
		};

		super.setHandler(handler);
	}

	private void init() {
		btnBack = (Button) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_btn_back);
		btnClean = (Button) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_btn_clearn);
		btnHemai = (Button) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_btn_hemai);
		btnNormalBuy = (Button) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_btn_normalBy);
		tvCountAndTime = (TextView) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_tv_countAndTime);
		btnBack.setOnClickListener(this);
		btnClean.setOnClickListener(this);
		btnHemai.setOnClickListener(this);
		btnNormalBuy.setOnClickListener(this);

		tvBetCount = (TextView) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_tv_betCount);
		tvMoney = (TextView) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_tv_money);
		tvBalance = (TextView) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_tv_balance);
		edtBeitou = (EditText) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_edt_beitou);

		linearList = (LinearLayout) view.findViewById(R.id.sportteryfootballshengfuchooseactlist_linear_list);

		sportteryFootballShengfuNormalBuyCommunicater = new SportteryFootballShengfuNormalBuyCommunicater();
		// 下面为倍数文本框 输入的倍数的 判断
		edtBeitou.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (!(String.valueOf(s).equals(Config.NULLMARK)) && s != null) {

					Toast partCountToast = Toast.makeText(context, Language.STR_QINGSHURU + Config.NUM1 + Config.HORIZONTAL_LINE + "100000" + Language.STR_ZHIJIANDESHUZI, Toast.LENGTH_SHORT);
					beishu = Integer.parseInt(String.valueOf(s));
					if (beishu > 100000) {
						partCountToast.show();
						beishu = 100000;
						edtBeitou.setText(Integer.toString(beishu));
					} else if (beishu < 1) {
						partCountToast.show();
						beishu = 1;
						edtBeitou.setText(Integer.toString(beishu));
					} else {

					}
				} else {
					beishu = 0;
				}
				updateTextViewSet();
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
		progressdialogNormalBuy = ViewerUtil.showProgressDialog(context, Language.POPLOTTERYTIP_BUYING);

	}

	/** 胜负彩复式代购接口 线程购买 */
	private void threadNormalBuy() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				try {
					long money = (long) beishu * (long) zhushu * 2;
					boolean b = sportteryFootballShengfuNormalBuyCommunicater.isBuySucceed(zhushu, beishu, money, codes);

					if (b) {
						Message msg = handlerChooseList.obtainMessage(BUY_SUCCEED, null);
						handlerChooseList.sendMessage(msg);
						UserInfo.getUserInfo().setUserBalance(sportteryFootballShengfuNormalBuyCommunicater.getResult().get(Key.KEY_USERMONEY));
					} else {
						Message msg = handlerChooseList.obtainMessage(BUY_FAIL, null);
						handlerChooseList.sendMessage(msg);
					}
				} catch (Exception e) {
					Message msg = handlerChooseList.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerChooseList.sendMessage(msg);
				}
			}
		}.start();
	}

	public View getView() {
		EDebug.o(this, "getview 被调用");
		String voteEndTime = SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getVoteEndTime();
		if (voteEndTime != null && !voteEndTime.equals(Config.NULLMARK)) {
			tvCountAndTime.setText("共14场比赛，截止时间：" + SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getVoteEndTime());
		} else {
			tvCountAndTime.setText("共14场比赛");
		}
		updateListView();
		dataInit();
		updateTextViewSet();
		edtBeitou.setText(Integer.toString(beishu));
		((SportteryChooseActivity) context).setCurrentPageFlag(1);
		Message msg = SportteryFootballShengfuAgp.handlerSportteryFootballShengfuAgp.obtainMessage(SportteryFootballShengfuAgp.IN_LIST_VIEW, null);
		SportteryFootballShengfuAgp.handlerSportteryFootballShengfuAgp.sendMessage(msg);
		return view;
	}

	public void setViewChoosePage(SportteryFootballShengfuChooseList choosePage) {
		this.chooseListPage = choosePage;
	}

	/** 界面点击事件处理 */
	@Override
	public void onClick(View v) {
		Message msg = SportteryFootballShengfuChooseList.handlerChoosePage.obtainMessage(SportteryFootballShengfuChooseList.DO_BACK_UPDATE, null);
		switch (v.getId()) {
		case R.id.sportteryfootballshengfuchooseactlist_btn_back:
			SportteryFootballShengfuChooseList.handlerChoosePage.sendMessage(msg);
			((Activity) context).setContentView(chooseListPage.getView());
			break;

		case R.id.sportteryfootballshengfuchooseactlist_btn_clearn:
			cleanTheList();
			SportteryFootballShengfuChooseList.handlerChoosePage.sendMessage(msg);
			((Activity) context).setContentView(chooseListPage.getView());
			break;

		case R.id.sportteryfootballshengfuchooseactlist_btn_hemai:
			checkHemai();
			break;

		case R.id.sportteryfootballshengfuchooseactlist_btn_normalBy:
			checkNomalBuy();
			break;

		default:
			break;
		}

	}

	/** 发起合买处理方法 */
	private void checkHemai() {
		updateNormalBy();
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().setCodes(codes);
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().setMultiple(beishu);
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().setZhushuCount(zhushu);
		Intent intentToHemai = new Intent();
		intentToHemai.setClass(context, SportteryFootballShengfuFaqiHemaiAct.class);
		if (beishu > 0 && zhushu > 0) {
			if (UserInfo.getUserInfo().getUserSession().length() < 3) {
				Intent intent = new Intent();
				intent.setClass(context, LoginAct.class);
				((Activity) context).startActivity(intent);
			} else {
				((Activity) context).startActivity(intentToHemai);
			}
		} else {
			ViewerUtil.showTipDialog(context, "请正确填写信息", false);
		}
	}

	/** 立即购买 处理方法 */
	private void checkNomalBuy() {
		if (updateNormalBy()) {
			if (beishu > 0 && zhushu > 0) {
				if (UserInfo.getUserInfo().getUserSession().length() < 3) {
					Intent intent = new Intent();
					intent.setClass(context, LoginAct.class);
					((Activity) context).startActivity(intent);
				} else {
					LoginTimeControl test = new LoginTimeControl();
					if (test.isNeedInputPwd()) {
						test.showInputPasswordDialog(context);
					} else {
						progressdialogNormalBuy.show();
						threadNormalBuy();
					}
				}
			} else {
				ViewerUtil.showTipDialog(((Activity) context), "请正确填写信息", false);
			}
		}
	}

	/**
	 * 点击立即购买时 发起和买的时候 需要处理的一些事项
	 */
	private boolean updateNormalBy() {

		if (this.getActiveItemNum() < allTeam) {
			ViewerUtil.showTipDialog(((Activity) context), "请选择至少14队伍", false);
			return false;
		}

		StringBuffer sbCodes = new StringBuffer();
		for (int i = 0; i < SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(W)) {
					sbCodes.append("3");
				}
				if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(D)) {
					sbCodes.append("1");
				}
				if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(L)) {
					sbCodes.append("0");
				}
				sbCodes.append(Config.COMMA);
			}

		}
		sbCodes.deleteCharAt(sbCodes.length() - 1);
		codes = sbCodes.toString();
		EDebug.o(SportteryFootballShengfuBuyLottery.this, "the codes is " + codes);
		zhushu = getZhushuCount(allTeam, SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList());
		EDebug.o(SportteryFootballShengfuBuyLottery.this, "the betCount is " + zhushu);
		return true;
	}

	/** 更新textview显示 */
	private void updateTextViewSet() {
		EDebug.o(this, "update text view 被调用");
		zhushu = getZhushuCount(allTeam, SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList());
		tvBetCount.setText(Integer.toString(zhushu));
		DecimalFormat f = new DecimalFormat("0.00");
		tvMoney.setText(Language.POPLOTTERYTIP_RMB + f.format((long) zhushu * (long) beishu * 2));
		for (int i = 0; i < SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				break;
			}
		}
		EDebug.o(this, "UserInfo.getUserInfo().getUserBalance() = " + UserInfo.getUserInfo().getUserBalance() + " zhushu = " + zhushu + " beishu = " + beishu);
		if (UserInfo.getUserInfo().getUserBalance() == null || UserInfo.getUserInfo().getUserBalance().equals(Config.NULLMARK)) {
			tvBalance.setText(":获取失败");
		} else {
			tvBalance.setText(Language.POPLOTTERYTIP_RMB + com.esun.util.LotteryUtils.getLotRestBottom(UserInfo.getUserInfo().getUserBalance(), zhushu * beishu * 2));
		}
	}

	/** 获取注数 */
	private int getZhushuCount(int activeTeamCount, ArrayList<Hashtable<String, Object>> lotteryList) {
		int zhushu_ = 0;
		if (activeTeamCount == allTeam) {
			int[] pDetail = new int[allTeam];
			int sum = 1;
			for (int i = 0; i < allTeam; i++) {
				if ((Boolean) lotteryList.get(i).get(IS_ACTIVE)) {
					int checkCount = 0;
					if ((Boolean) lotteryList.get(i).get(W)) {
						checkCount += 1;
					}
					if ((Boolean) lotteryList.get(i).get(D)) {
						checkCount += 1;
					}
					if ((Boolean) lotteryList.get(i).get(L)) {
						checkCount += 1;
					}
					pDetail[i] = checkCount;
				} else {

					return pDetail[i] = 0;

				}
				sum = sum * pDetail[i];
			}
			zhushu_ = sum;
		} else {
			zhushu_ = 0;
		}
		return zhushu_;
	}

	/** 清除投注列表 */
	private void cleanTheList() {
		linearList.removeAllViews();
		for (int i = 0; i < SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).put(IS_ACTIVE, false);
				SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).put(W, false);
				SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).put(D, false);
				SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).put(L, false);
			}
		}
	}

	/** 更新第position个投注队伍 */
	private void updateLotteryListItem(int position, Boolean w, Boolean d, Boolean l) {
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(position).put(IS_ACTIVE, true);
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(position).put(W, w);
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(position).put(D, d);
		SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(position).put(L, l);
		if (!w && !d && !l) {
			SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(position).put(IS_ACTIVE, false);
			SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(position).put(IS_DAN, false);

		}
		EDebug.o(this, SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getActiveTeamCount());
		EDebug.o(SportteryFootballShengfuBuyLottery.this, "the active item count is " + SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getActiveTeamCount());
		updateTextViewSet();
	}

	/** 获取可用队伍个数 */
	public int getActiveItemNum() {
		int count = 0;
		for (int i = 0; i < SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().size(); i++) {
			if ((Boolean) SportteryFootballShengfuLotteryInfo.getSportteryFootballShengfuLotteryInfo().getLotterList().get(i).get(IS_ACTIVE)) {
				count++;
			}
		}
		return count;
	}

	private Dialog dialogBuySucceed;

	/** 购买成功 */
	private void buySucceed() {
		dialogBuySucceed = new Dialog(context, R.style.dialog);
		dialogBuySucceed.setContentView(R.layout.viewerutil_dialog_showbuysucceed);
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_msg)).setText("让球胜平负复式代购成功");

		DecimalFormat f = new DecimalFormat("0.00");
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_term)).setText("注数:" + Integer.toString(zhushu));
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_count)).setText("倍投:" + Integer.toString(beishu));

		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_cost)).setText("消费:" + Language.POPLOTTERYTIP_RMB + f.format((long) zhushu * (long) beishu * 2));
		((TextView) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_tv_balance)).setText("剩余金额:" + Language.POPLOTTERYTIP_RMB + UserInfo.getUserInfo().getUserBalance());
		dialogBuySucceed.show();
		OnClickListener dialogSucceedListener = new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				SportteryFootballLotteryInfo.getSportteryFootballLotteryInfo().getLotterList().clear();

				switch (v.getId()) {
				case R.id.viewerutildialogshowbuysucceed_btn_back:
					dialogBuySucceed.dismiss();
					Message msg = SportteryFootballShengfuChooseList.handlerChoosePage.obtainMessage(SportteryFootballShengfuChooseList.DO_REFRESH, null);
					SportteryFootballShengfuChooseList.handlerChoosePage.sendMessage(msg);
					((Activity) context).setContentView(chooseListPage.getView());
					break;

				case R.id.viewerutildialogshowbuysucceed_btn_toHall:
					dialogBuySucceed.dismiss();
					SportteryFootballShengfuChooseAct.pageFlag = 0;

					((Activity) context).getParent().finish();
					break;

				default:
					break;
				}

			}
		};
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_back)).setOnClickListener(dialogSucceedListener);
		((Button) dialogBuySucceed.getWindow().findViewById(R.id.viewerutildialogshowbuysucceed_btn_toHall)).setOnClickListener(dialogSucceedListener);
		dialogBuySucceed.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {

			}
		});

	}

	private Dialog dialogBalanceNotEnough;
	private View.OnClickListener moneyNotEnoughListener;

	/** 余额不足提示 */
	private void popBalanceNotEnoughAlert() {
		if (dialogBalanceNotEnough == null) {
			dialogBalanceNotEnough = new Dialog(context, R.style.dialog);
		}

		if (moneyNotEnoughListener == null) {
			moneyNotEnoughListener = new OnClickListener() {

				@Override
				public void onClick(View v) {
					switch (v.getId()) {
					case R.id.shiyixuanwu_alert_money_not_enough_btn_cancel:
						break;

					case R.id.shiyixuanwu_alert_money_not_enough_btn_recharge:
						UserInfo.getUserInfo().setBuyingRecharge(true);

						((Activity) context).startActivity(new Intent().setClass(context, RechargeMenuPage.class));

						break;
					}

					dialogBalanceNotEnough.dismiss();
				}
			};
		}

		View view = ((Activity) context).getLayoutInflater().inflate(R.layout.shiyixuanwu_alert_money_not_enough, null);

		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_cancel)).setOnClickListener(moneyNotEnoughListener);
		((Button) view.findViewById(R.id.shiyixuanwu_alert_money_not_enough_btn_recharge)).setOnClickListener(moneyNotEnoughListener);

		dialogBalanceNotEnough.setContentView(view);
		dialogBalanceNotEnough.show();
	}

	@Override
	public void setSportteryChooseListPage(SportteryChooseList chooseListPage) {
		this.chooseListPage = chooseListPage;
	}

	/** 2012-07-04 billy add for refresh user balance */
	private void threadGetUserBalance() {
		new Thread() {
			@Override
			public void run() {
				EDebug.o(this, "开始获取用户余额的线程3");
				try {
					if (UserInfo.getUserInfo().getUserSession().length() < 1) {
						EDebug.o(this, "用户未登录，故取消获取金额");
						Message msg = handlerBalance.obtainMessage(GETBALANCE_FAIL, null);
						handlerBalance.sendMessage(msg);
					} else {
						if (userCommunicater == null) {
							userCommunicater = new UserCommunicater();
						}
						Boolean b = userCommunicater.isLoginSucceed(UserInfo.getUserInfo().getUserName(), UserInfo.getUserInfo().getUserPwd());
						EDebug.o(this, "获取余额是否成功 = " + b);
						if (b) {
							Message msg = handlerBalance.obtainMessage(GETBALANCE_SUCCEED, null);
							handlerBalance.sendMessage(msg);
						} else {
							Message msg = handlerBalance.obtainMessage(GETBALANCE_FAIL, null);
							handlerBalance.sendMessage(msg);
						}
					}
				} catch (Exception e) {
					Message msg = handlerBalance.obtainMessage(Config.CONNECTION_FAIL, null);
					handlerBalance.sendMessage(msg);
				}
			}
		}.start();
	}

	private void initHandler() {
		handlerBalance = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {

				case Config.CONNECTION_FAIL:
					EDebug.o(this, "更新用户余额异常");
					break;

				case GETBALANCE_SUCCEED:
					EDebug.o(this, "更新用户余额成功");
					userCommunicater.updataUserInfo();
					break;

				case GETBALANCE_FAIL:
					EDebug.o(this, "更新用户余额失败");
					break;

				default:
					break;
				}
			}
		};
	}

}
